﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data.OleDb;
using System.Data;
using TuongLaiDangCho.DTO;

namespace TuongLaiDangCho.DAO
{
    class UserDAO
    {
        public static bool isLogin(UserDTO user)
        {
            user.PassWork = GetMd5Hash(user.PassWork);
            string Sql = "Select * From [User] Where PassWork='" + user.PassWork + "'";
            OleDbConnection cn = default(OleDbConnection);
            cn = DataProvider.OpenCn();
            OleDbDataAdapter da = new OleDbDataAdapter(Sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            if ((dt.Rows.Count > 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void UpdatePW(UserDTO users)
        {
            users.PassWork = GetMd5Hash(users.PassWork);
            string Sql = "Update [User] Set Passwork='" + users.PassWork+ "'";
            OleDbConnection cn = default(OleDbConnection);
            cn = DataProvider.OpenCn();
            OleDbCommand cm = new OleDbCommand(Sql, cn);
            cm.ExecuteNonQuery();
            cn.Close();
        }

        public static void UpdateSkin(UserDTO gid)
        {
            string Sql = "Update [User] Set GiaoDien='" + gid.GiaoDien + "', DanhNgonIndex="+gid.DanhNgonIndex.ToString()+"";
            OleDbConnection cn = default(OleDbConnection);
            cn = DataProvider.OpenCn();
            OleDbCommand cm = new OleDbCommand(Sql, cn);
            cm.ExecuteNonQuery();
            cn.Close();
        }

        public static DataTable LoadSkin()
        {
            string Sql = "Select GiaoDien, DanhNgonIndex From [USER]";
            OleDbConnection cn = new OleDbConnection();
            cn = DataProvider.OpenCn();
            OleDbDataAdapter da = new OleDbDataAdapter(Sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable LoadLock()
        {
            string sql = "Select TimeLock, NumberInvalid From [USER]";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            return dt;
        }

        public static void UpdateLock(UserDTO user)
        {
            string sql="Update [USER] set TimeLock="+user.TimeLock.ToString()+" , NumberInvalid="+user.NumberInvalid.ToString()+"";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbCommand cm = new OleDbCommand(sql, cn);
            cm.ExecuteNonQuery();
            cn.Close();
        }

        public static void UpdateDanhNgonIndex(UserDTO user)
        {
            string sql = "Update [USER] set DanhNgonIndex=" + user.DanhNgonIndex.ToString() + "";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbCommand cm = new OleDbCommand(sql, cn);
            cm.ExecuteNonQuery();
            cn.Close();
        }

        public static DataTable LoadDanhNgonIndex()
        {
            string sql = "Select DanhNgonIndex From [USER]";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            return dt;
        }
        public static string GetMd5Hash(string input)
        {
            MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            int i = 0;
            for (i = 0; i <= data.Length - 1; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        //GetMd5Hash
    }
}
